var app = new Vue({
    el: "#xp-wrapper",
    data: {
        loginname: "",
        username: "",
        sex: "",
        email: "",
        phone:"",
        code:"",
        newPassword:""
    },
    mounted:function(){
        this.userinfo();
    },
    methods: {
        userinfo:function (){
            axios({
                method:'post',
                url:"yly-api/user/logon/queryUserInfo",
                headers:{'token': localStorage.token},
            }).then((res)=>{
                console.log(res);
                let account=res.data.data;
                if (res.data.code==200){
                    this.loginname=account.loginname;
                    this.username=account.username;
                    if (account.sex==1){
                        this.sex="男"
                    }else {
                        this.sex="女"
                    }
                    this.email=account.email;
                    this.phone=account.phone;
                }else if (res.data.code==401){
                    alert(res.data.msg);
                    location.href="index.html";
                }else if (res.data.code==500){
                    location.href="../../500.html";
                }
            }).catch(error=>{
                console.log(error.response.status);
                if (error.response.status==401){
                    alert("请先登录!");
                    location.href="index.html";
                }else if (error.response.status==403){
                    alert("用户已过期，请重新登录！");
                    localStorage.token=null;
                    location.href="index.html";
                }

            });
        },
        gain:function (){
            alert("已发送验证码，请稍后在手机上查看！");
            let data=new URLSearchParams();
            data.append("phone",this.phone);
            axios({
                method: "post",
                url:"yly-api/user/logon/send",
                data: data,
                headers: {"token":localStorage.token}
            }).then((res)=>{
                console.log(res.data);
                if (res.data.code==200){

                    localStorage.code=res.data.data.code;

                }else if (res.data.code==500){
                    location.href="../../500.html";
                }
            }).catch(error=>{
                console.log(error.response.status);
                if (error.response.status==401){
                    alert("请先登录!");
                    location.href="index.html";
                }else if (error.response.status==403){
                    alert("用户已过期，请重新登录！");
                    localStorage.token=null;
                    location.href="index.html";
                }
            });
        },
        verify:function (){
            if (this.code!=localStorage.code){
                alert("验证码有误！");
                this.code="";
            }
        },
        xiugai:function (){
            if (this.code!=null && this.code!="" && this.code!=undefined){
                this.xiugaimima();
            }else {
                $(".xianshi").attr("style","display:block");
            }
        },
        xiugaimima:function (){
            let data=new URLSearchParams();
            data.append("newPassword",this.newPassword);
            axios({
                method:"post",
                url:"yly-api/user/logon/change",
                data:data,
                headers:{"token":localStorage.token}
            }).then(function (res){
                console.log(res);
                if (res.data.code==200){
                    alert("修改成功,请重新登录！");
                    localStorage.token=null;
                    location.href="index.html";
                }else if (res.data.code==500){
                    location.href="../../500.html";
                }

            }).catch(function (error){
                console.log(error.response.status);
                if (error.response.status==401){
                    alert("请先登录!");
                    location.href="index.html";
                }else if (error.response.status==403){
                    alert("用户已过期，请重新登录！");
                    localStorage.token=null;
                    location.href="index.html";
                }
            });
        }
    }
});